<template>
  <div id="app">
   <a-input placeholder="请输入任务" class="my_ipt" :value='inputValue' @change='inputChange'/>
   <a-button type="primary" @click="addItemToList">添加事项</a-button>
   <!-- 下半部分 -->
   <a-list bordered :dataSource=infoList class="dt_list">
        <a-list-item slot="renderItem" slot-scope="item">
          <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="(e) => {cbStatusChange(e,item.id)}">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click='removeItemById(item.id)'>删除</a>
      </a-list-item>
      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <span>{{unDoneLength}}条剩余</span>
        <a-button-group>
          <a-button :type="btnKey === 'all' ? 'primary' : 'default'" @click="changeBtnVal('all')">全部</a-button>
          <a-button :type="btnKey === 'undone' ? 'primary' : 'default'" @click="changeBtnVal('undone')">未完成</a-button>
          <a-button :type="btnKey === 'done' ? 'primary': 'default'" @click="changeBtnVal('done')">已完成</a-button>
        </a-button-group>
        <a @click="clear">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {
    }
  },
  created () {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['inputValue', 'btnKey']),
    ...mapGetters(['unDoneLength', 'infoList'])
  },
  methods: {
    // 监听文本框输入变化
    inputChange (e) {
      // console.log(e.target.value)
      this.$store.commit('setInputValue', e.target.value)
    },
    // 向列表中新增 item 项
    addItemToList () {
      if (this.inputValue.trim().length <= 0) {
        return this.$message.warning('文本框内容不能为空')
      }
      this.$store.commit('addItem', this.inputValue.trim())
    },
    removeItemById (id) {
      // console.log(id)
      this.$store.commit('removeItemById', id)
    },
    cbStatusChange (e, id) {
      const param = {
        id: id,
        status: e.target.checked
      }
      this.$store.commit('statusChange', param)
    },
    clear () {
      this.$store.commit('clearDone')
    },
    changeBtnVal (val) {
      this.$store.commit('changeBtnVal', val)
    }
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}
.my_ipt{
  width: 500px;
  margin-top: 10px;
}
.dt_list{
  width: 500px;
  margin-top: 10px;
}
.footer{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
